﻿import cgi
import os
from google.appengine.ext.webapp import template
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class user (db.Model):
  name = db.StringProperty(required=True)
  pwd = db.StringProperty(required=True)
  type = db.StringProperty(required=True, choices=set(["zly", "gly", "syy","sgy","gjy","jdy"]))


class login(webapp.RequestHandler):
  def get(self):
    User=db.GqlQuery("SELECT * FROM user WHERE name = :1",
                      self.request.get('name')).get()
    if (User==None):
      self.response.out.write("""username err <p> <a href="/">back</a>""")
    else:
      if (User.pwd==self.request.get('pwd')):
        template_values = {
          'name': User.name,
          'pwd': User.pwd,
          'type':User.type,
        }
        path = os.path.join(os.path.dirname(__file__), 'login.html')
        self.response.out.write(template.render(path, template_values))
      else:
        self.response.out.write("""pwd err! <p> <a href="/">back</a>""")

class MainPage(webapp.RequestHandler):
  def get(self):
    template_values = {
      'name':"name",
      'pwd':"pwd",
    }
    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

class admin(webapp.RequestHandler):
  def get(self):
    users_query = user.all().order('-name')
    users = users_query.fetch(10)

    template_values = {
      'users':users,
    }
    path = os.path.join(os.path.dirname(__file__), 'admin.html')
    self.response.out.write(template.render(path, template_values))

  def post(self):
    User = user(name=self.request.get('name'),
                pwd=self.request.get('pwd'),
                type=self.request.get('type'))
    User.put()
    self.response.out.write("""ok! <p> <a href="/admin">back</a>""")
    






application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/login',login),
                                      ('/admin',admin)],
                                     debug=True)
def main():
  run_wsgi_app(application)
if __name__ == "__main__":
  main()

